package com.itcncp.ipaybox.server.server;

import com.itcncp.ipaybox.server.service.NettyMsgResolveService;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.SocketChannel;

/**
 * 消息通道Handler
 * @author LiuPeng
 * 
 */
public class NettyServerHandler extends SimpleChannelInboundHandler<ByteBuf> {

	private NettyMsgResolveService nettyMsgResolveService;

	public NettyServerHandler(NettyMsgResolveService nettyMsgResolveService) {
		this.nettyMsgResolveService = nettyMsgResolveService;
	}

	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		NettyChannelMap.remove((SocketChannel) ctx.channel());
	}

	@Override
	protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception {
		nettyMsgResolveService.resolve(channelHandlerContext, byteBuf);
//		ReferenceCountUtil.release(byteBuf);
	}
}
